Completed
Push — master ( 7f61c2...b64a7c )
by D
02:36
created

Lightbox.initHTML   A

Complexity

Conditions 1

Size

Total Lines 26
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
dl 0
loc 26
rs 9.304
c 0
b 0
f 0
1
import {define} from "../globals/globals";
2
import {LightboxInterface} from "../interfaces/LightboxInterface";
3
import {LightboxOptions} from "../types/LightboxOptions";
4
import {ImagePositionOptions} from "../types/internal/ImagePositionOptions";
5
6
(function (root, factory) {
7
    if (typeof define === 'function' && define.amd) {
8
        define([], factory);
9
    } else if (typeof module === 'object' && module.exports) {
10
        module.exports = factory();
11
    } else {
12
        root.Lightbox = factory();
13
    }
14
}(typeof self !== 'undefined' ? self : this, function () {
15
16
    class Lightbox implements LightboxInterface {
17
        public options: LightboxOptions;
18
19
        constructor(options: LightboxOptions) {
20
21
            if (!options.targets) {
22
                throw new Error('No targets')
23
            }
24
25
            this.options = options;
26
            this.options.targets = typeof options.targets === 'string' ? document.querySelectorAll(options.targets) : options.targets;
27
            this.initClasses();
28
            this.initClone();
29
            this.initClose();
30
        }
31
32
        private initClasses(): void {
33
            this.options.targets.forEach(function (target: HTMLImageElement) {
34
                target.classList.add('lightbox');
35
            });
36
        }
37
38
        private initHTML(target: HTMLImageElement, position: ImagePositionOptions): void {
39
            let wrapper = document.createElement('div');
40
            wrapper.classList.add('lightbox-clone-wrapper');
41
42
            setTimeout(() => {
43
                wrapper.classList.add('active');
44
            }, 100);
45
46
            let clone = document.createElement('div');
47
            clone.classList.add('lightbox-clone');
48
            clone.style.top = position.top + 'px';
49
            clone.style.left = position.left + 'px';
50
            clone.style.width = target.naturalWidth + 'px';
51
52
            setTimeout(() => {
53
                clone.classList.add('centered');
54
            }, 100);
55
56
            let cloneImg = document.createElement('img');
57
            cloneImg.src = target.src;
58
59
            clone.appendChild(cloneImg);
60
            wrapper.appendChild(clone);
61
            document.body.appendChild(wrapper);
62
        }
63
64
        private initClone(): void {
65
            let that = this;
66
67
            this.options.targets.forEach(function (target: HTMLImageElement) {
68
                target.addEventListener('click', function (e) {
69
                    that.initHTML(e.target as HTMLImageElement, Lightbox.getTargetPosition(target));
70
                })
71
            });
72
        }
73
74
        static getTargetPosition(target: HTMLImageElement): ImagePositionOptions {
75
            let targetPosition = target.getBoundingClientRect();
76
77
            return {
78
                top: targetPosition.top,
79
                left: targetPosition.left
80
            }
81
        }
82
83
        protected initClose(): void {
84
            this.bodyClose();
85
            this.scrollClose();
86
        }
87
88
        static close(): void {
89
            let wrapper = document.querySelector('.lightbox-clone-wrapper');
90
91
            if(wrapper) {
92
                let clone = wrapper.querySelector('.lightbox-clone')!;
93
                clone.classList.remove('centered');
94
95
                setTimeout(() => {
96
                    wrapper!.remove();
97
                }, 500);
98
            }
99
        }
100
101
        protected bodyClose(): void {
102
            let that = this;
103
104
            document.addEventListener('click', function (e) {
105
                e.preventDefault();
106
107
                if((e.target as HTMLElement).closest('.lightbox-clone-wrapper')) {
108
                    Lightbox.close();
109
                }
110
            });
111
        }
112
113
        protected scrollClose(): void {
114
            let that = this;
115
116
            document.addEventListener('scroll', function () {
117
                Lightbox.close();
118
            });
119
        }
120
    }
121
122
    return Lightbox;
123
}));
124
125